/**
 *  Copyright 2002-2009 the original author or authors.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package foo.bar.blog.core.domain;

import foo.bar.blog.radeox.Decoratable;

import javax.persistence.*;
import java.util.Date;
import java.util.UUID;

/**
 * Representing a Comment domain object.
 * 
 * @author tmjee
 * @version $Date: 2009-06-06 13:26:52 +0000 (Sat, 06 Jun 2009) $ $Id$
 */
@Entity
@EntityListeners({EntityAuditListener.class})
@Table(name="TBL_COMMENT")
@NamedQueries({
    @NamedQuery(name="comment_findCommentById",
            query="SELECT comment FROM Comment AS comment where comment.id = :id")        
})
public class Comment implements CommentInterface, CreationInfoPersistable, Identifiable, Decoratable {

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "commentIdGenerator")
    @TableGenerator(name="commentIdGenerator", table = "TBL_ID_GENERATOR",
                    pkColumnName = "ID", valueColumnName = "ID_VALUE",
                    pkColumnValue = "TBL_COMMENT_CURRENT_ID", initialValue = 1,
                    allocationSize = 10)
    @Column(name="ID", nullable = false)
    private long id;

    @Basic
    @Column(name="CONTENT")
    private String content;

    @Basic
    @Column(name="CREATION_DATE")
    private Date creationDate;

    @ManyToOne(targetEntity = User.class)
    @JoinColumn(table = "TBL_COMMENT", name = "CREATOR_ID", referencedColumnName = "ID")
    private User creator;

    @ManyToOne(targetEntity = Entry.class)
    @JoinColumn(table="TBL_COMMENT", name = "ENTRY_ID", referencedColumnName = "ID")
    private Entry entry;

    @Basic
    @Column(name="UID")
    private String uid = UUID.randomUUID().toString();


    public void setCreationDate(Date creationDate) {
        this.creationDate = creationDate;
    }

    public Date getCreationDate() {
        return creationDate;
    }

    public void setCreator(User creator) {
        this.creator = creator;
    }

    public User getCreator() {
        return creator;
    }


    public void setId(long id) {
        this.id = id;
    }

    public long getId() {
        return id;
    }


    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Entry getEntry() {
        return entry;
    }

    public void setEntry(Entry entry) {
        this.entry = entry;
    }

    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Comment comment = (Comment) o;

        if (uid != null ? !uid.equals(comment.uid) : comment.uid != null) return false;

        return true;
    }

    public int hashCode() {
        int result;
        result = (int) (id ^ (id >>> 32));
        result = 31 * result + (content != null ? content.hashCode() : 0);
        result = 31 * result + (creationDate != null ? creationDate.hashCode() : 0);
        result = 31 * result + (creator != null ? creator.hashCode() : 0);
        return result;
    }
}
